import { routes } from "@/router";
import { createSlice } from "@reduxjs/toolkit";
import type { RouteObject } from "react-router-dom";
import _ from "lodash";



function generateRoutes(routes: RouteObject[]) {
    function filterIndex(routes: RouteObject[]) {
        const cloneRoutes = _.cloneDeep(routes);
        const newRoutes = cloneRoutes.filter((route) => !route.index);
        newRoutes.forEach(route => {
            if (route.children) {
                route.children = filterIndex(route.children)
            }
            if (route.children && route.children.length === 0) {
                route.children = undefined;
            }
        })
        //排序
        return newRoutes.sort((a, b) => (b.sort || 0) - (a.sort || 0));
    }
    return filterIndex(routes)
}

const userSlice = createSlice({
    name: 'user',
    initialState: {
        username: "张三",
        menuItems: generateRoutes(routes),//用于Menu组件的items的选项
    },
    reducers: {},
})
export default userSlice.reducer;
